package com.aaa.controller.user;

import com.aaa.entity.Card;
import com.aaa.entity.ResponseDto;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.aaa.service.impl.UserServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//删除会员
@WebServlet("/DeleteUserServlet")
public class DeleteUserServlet extends HttpServlet {
        UserService userService;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String userId = req.getParameter("userId");
            User user = new User();
            user.setUserId(Integer.valueOf(userId));
            Card card = new Card();
            card.setUserId(Integer.valueOf(userId));

            int num = userService.deleteByuserId(user,card);

            ResponseDto responseDto = new ResponseDto();
            if (num > 0){
                responseDto.setStatus(ResponseDto.SUCCESS_CODE);
                responseDto.setMessage("删除成功");
            }else {
                responseDto.setStatus(ResponseDto.FAILURE_CODE);
                responseDto.setMessage("删除失败");
            }
            resp.getWriter().write(new Gson().toJson(responseDto));

        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }

        @Override
        public void init(ServletConfig config) throws ServletException {
            userService = new UserServiceImpl();
        }
}

